package com.emip.servlet.image;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emip.bean.ImageBean;
import com.emip.pojo.Image;
import com.emip.pojo.Table;
import com.emip.service.ImageService;
import com.emip.util.ReportFormat;


@Controller
@RequestMapping(value="/image/data")
public class ImageDataAction {
	
	@Autowired
	ImageService imageService;
	
	SimpleDateFormat df = new SimpleDateFormat(ReportFormat.SECONDFORMAT);

	@RequestMapping(value="/show")
	public String show() {
		return "image/image";
	}
	
	@RequestMapping(value="/search")
	@ResponseBody
	public Table<Image> search(
			@RequestParam("startDate") String startDate, 
			@RequestParam("endDate") String endDate,
			@RequestParam("siteId") String siteId,
			@RequestParam("page") int page,
			@RequestParam("rows") int size) {
		List<ImageBean> imageBeans = 
				imageService.findByDate(siteId, startDate, endDate, page, size);
		long count = imageService.getCount(siteId, startDate, endDate);
		Table<Image> imgTable = new Table<Image>();
		imgTable.setPage(page);
		imgTable.setRecords(count);
		imgTable.setTotal(count/size + (count%size==0?0:1));
		List<Image> imgRows = new ArrayList<Image>();
		for (int i = 0; i < imageBeans.size(); i++) {
			ImageBean imageBean = imageBeans.get(i);
			Image sImg = new Image();
			sImg.setSrc(imageBean.getUri());
			sImg.setTitle(df.format(imageBean.getGenTime()));
			Image bImg = new Image();
			bImg.setSrc(imageBean.getUri());
			sImg.getReference().put("bigImg", bImg);
			imgRows.add(sImg);
		}
		imgTable.setRows(imgRows);
		return imgTable;
	}
}
